package com.next

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
@Transactional(readOnly = true)
class ProductLineController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService
    def exportService

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond ProductLine.list(params), model: [productLineCount: ProductLine.count()]
    }

    def show(ProductLine productLine)
    {
        respond productLine
    }

    def create()
    {
        respond new ProductLine(params)
    }

    @Transactional
    def save(ProductLine productLine)
    {
        if (productLine == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (productLine.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond productLine.errors, view: 'create'
            return
        }

        productLine.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'productLine.label', default: 'ProductLine'), productLine.id])
                redirect productLine
            }
            '*' { respond productLine, [status: CREATED] }
        }
    }

    def edit(ProductLine productLine)
    {
        respond productLine
    }

    @Transactional
    def update(ProductLine productLine)
    {
        if (productLine == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (productLine.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond productLine.errors, view: 'edit'
            return
        }

        productLine.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'productLine.label', default: 'ProductLine'), productLine.id])
                redirect productLine
            }
            '*' { respond productLine, [status: OK] }
        }
    }

    @Transactional
    def delete(ProductLine productLine)
    {

        if (productLine == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        productLine.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'productLine.label', default: 'ProductLine'), productLine.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'productLine.label', default: 'ProductLine'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    @Transactional
    def searchProductLine()
    {
        def name = params["name"]
        String sql = "from ProductLine as u where 1=1"
        if (name)
        {
            sql += " and u.name like '%${name}%'"
        }
        String countSql = "select count(u.id) " + sql
        sql += ' order by createdDate desc'

        def list = ProductLine.findAll(sql, [max: params.max ?: 10, offset: params.offset ?: 0])
        def count = ProductLine.executeQuery(countSql)

        respond list, model: [productLineCount: count[0], params: params], view: 'index'
    }

    def exportProductLine()
    {
        def name = params["name"]
        String sql = "from ProductLine as u where 1=1"
        if (name)
        {
            sql += " and u.name like '%${name}%'"
        }
        String countSql = "select count(u.id) " + sql
        sql += ' order by createdDate desc'

        def list = ProductLine.findAll(sql)
        def count = ProductLine.executeQuery(countSql)

        if (params?.f && params.f != "html")
        {
            response.contentType = grailsApplication.config.grails.mime.types[params.f]
            response.setHeader("Content-disposition", "attachment; filename=productLine.${params.extension}")
            List fields = ["name", "createdDate", "modifiedDate"]
            Map labels = ["name": "名称", "createdDate": "创建时间", "modifiedDate": "修改时间"]
            exportService.export(params.f, response.outputStream, list, fields, labels, [:], [:])
        }
    }
}
